package com.five.admin.service.impl;

import com.five.admin.dao.ChapterMapper;
import com.five.admin.dao.CourseMapper;
import com.five.admin.service.CourseService;

import com.five.entity.Chapter;
import com.five.entity.Course;
import com.five.entity.qo.CourseQo;
import com.five.entity.qo.InsertCourseQo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: star
 * @Date: 2021-05-21 14:01
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ChapterMapper chapterMapper;

    /**
     * @return 课程列表
     */
    @Override
    public List<CourseQo> getCourseList() {
        List<CourseQo> courseList = courseMapper.selectCourseQo();
        return courseList;
    }

    /**
     * 添加课程
     * @param insertCourseQo 课程名、课程类型、价格、图片、状态
     */
    @Override
    public void insertCourse(InsertCourseQo insertCourseQo) {
        courseMapper.insertCourse(insertCourseQo);
    }

    /**
     * 删除课程
     * @param course id
     */
    @Override
    public void deleteCourse(Course course) {
        Long id = course.getId();
        Chapter chapter = new Chapter();
        chapter.setCourseId(id);
        List<Chapter> chapters = chapterMapper.selectByCourseId(chapter);
        if (chapters.size()==0){
            courseMapper.deleterCourseNoChapter(course);
        }else {
            courseMapper.deleteCourse(course);
        }
    }

    /**
     * 模糊查询课程
     * @param courseQo title
     * @return 课程列表
     */
    @Override
    public List<CourseQo> fuzzyQueryByTitle(CourseQo courseQo) {
        List<CourseQo> courseQos = courseMapper.fuzzyQueryByTitle(courseQo);
        return courseQos;
    }

    /**
     * 通过id查询课程信息
     * @param courseQo
     * @return
     */
    @Override
    public CourseQo selectById(CourseQo courseQo) {
        CourseQo resultCourseQo = courseMapper.selectById(courseQo);
        return resultCourseQo;
    }

    /**
     * 修改课程信息
     * @param insertCourseQo
     */
    @Override
    public void updateCourse(InsertCourseQo insertCourseQo) {
        courseMapper.updateCourse(insertCourseQo);
    }
}
